/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package session;

import entity.Customer;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Lisha
 */
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {
    @PersistenceContext(unitName = "emangaPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CustomerFacade() {
        super(Customer.class);
    }
    
    public void updateCustomer(HttpServletRequest request, Customer customer)
    {
        em.createNativeQuery("UPDATE customer SET phone='" + request.getParameter("phone").replaceAll("'", "\'\'") + "', address='" + request.getParameter("address").replaceAll("'", "\'\'") + "' WHERE id=" + customer.getId().toString()).executeUpdate();
    }
    
    public void insertCustomer(HttpServletRequest request)
    {
        em.createNativeQuery("INSERT INTO customer VALUES ('0', '" + request.getParameter("fname").replaceAll("'", "\'\'") + "', '" + request.getParameter("lname").replaceAll("'", "\'\'") + "', '" + request.getParameter("email").replaceAll("'", "\'\'") + "', '" + request.getParameter("password").replaceAll("'", "\'\'") + "', '" + request.getParameter("phone").replaceAll("'", "\'\'") + "', '" + request.getParameter("address").replaceAll("'", "\'\'") + "')").executeUpdate();
    }
    
    public Customer findByEmail(String s) {
        Query q = em.createNamedQuery("Customer.findByEmail");
        q.setParameter("email", s);
        return (Customer)q.getSingleResult();
    }
    
    public Customer findForConnection(String email, String password) {
        Query q = em.createNamedQuery("Customer.findForConnection");
        q.setParameter("email", email);
        q.setParameter("password", password);
        return (Customer)q.getSingleResult();
    }
    
}
